//
// Created by amon.huang on 2021/7/25.
//

#ifndef AMONPLAYER_FFMPEG_H
#define AMONPLAYER_FFMPEG_H

#include "JNICall.h"
#include "Audio.h"
#include "Video.h"
#include <pthread.h>
#include <fstream>

#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <cassert>

extern "C" {
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "libavutil/error.h"
};


class FFMpeg {
public:
    pthread_t readThreadT;
    pthread_mutex_t mutex;
    AVFormatContext *pFormatContext = nullptr;
    JNICall* pJniCall;
    char* url;

    PlayerStatus* pPlayerStatus;
    Audio *pAudio = nullptr;
    Video *pVideo = nullptr;

public:
    FFMpeg(JNICall *pJniCall);
    FFMpeg(JNICall *pJniCall, const char* url);
    ~FFMpeg();

    void play();

    void prepare();

    void prepareAsync();

    void prepare(ThreadMode threadMode);

    void callPlayJniError(ThreadMode threadMode, int code, char* msg);

    void release();

    void seek(uint64_t second) const;

    void setSurface(jobject pJobject) const;

    void setUrl(const char* pUrl);
};


#endif //AMONPLAYER_FFMPEG_H
